package org.lex.swing.widgets;

import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;

import javax.swing.JLabel;

import org.lex.swing.Swings;


public class GradientPaintLabel extends JLabel {
	private Color c1 = null;
	private Color c2 = null;
	private Color c3 = null;
	private Color c4 = null;
	private Point2D p1 = new Point2D.Float();
	private Point2D p2 = new Point2D.Float();
	private Rectangle2D rect = new Rectangle2D.Float();

	public static void main(String[] args) {
		// color model in windows-media-center title bar
		Color c1 = new Color(153, 198, 241);
		Color c2 = new Color(85, 147, 222);
		Color c3 = new Color(57, 128, 210);
		Color c4 = new Color(44, 85, 177);
		GradientPaintLabel label = new GradientPaintLabel(c1, c2, c3, c4);
		Swings.show(label);
	}

	public GradientPaintLabel(Color c1, Color c2, Color c3, Color c4) {
		super();
		if (null == c1 || null == c2 || null == c3 || null == c4)
			throw new NullPointerException("color is null");
		this.c1 = c1;
		this.c2 = c2;
		this.c3 = c3;
		this.c4 = c4;
	}

	/**
	 * <pre>
	 * +-----------------+
	 * |  c1
	 * |  v
	 * |  c2
	 * |.................
	 * |  c3
	 * |  v
	 * |  c4
	 * +-----------------+
	 * </pre>
	 */
	@Override
	protected void paintComponent(Graphics g) {
		float width = this.getWidth();
		float height = this.getHeight();
		p1.setLocation(0, 0);
		p2.setLocation(0, height / 2);
		Graphics2D g2 = (Graphics2D) g;
		GradientPaint paint = new GradientPaint(p1, c1, p2, c2);
		g2.setPaint(paint);
		rect.setRect(0, 0, width, height / 2);
		g2.fill(rect);
		// =======================================
		p1.setLocation(0, height);
		paint = new GradientPaint(p2, c3, p1, c4);
		g2.setPaint(paint);
		rect.setRect(0, height / 2, width, height / 2);
		g2.fill(rect);
		super.paintComponent(g2);
	}
}
